package com.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;
import com.util.SessionUtils;

public class BaseAction extends Action {

	public ActionForward doProcess(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return null;

	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward actionForward = null;
		try {
			String operationType = CommonTools.null2String(request.getParameter("operationType"));
			if("create".equals(operationType)){
				saveToken(request);
			}
			if("insert".equals(operationType)){
				if(isTokenValid(request, true)){
					actionForward = doProcess(mapping, form, request, response);
				}
			}else {
			   actionForward = doProcess(mapping, form, request, response);
			}
		} catch (Exception e) {	
			throw e;
		} finally {
		}
		return actionForward;
	}
	
	public T_S_YHCZXXinfo setYhczxx(HttpServletRequest request, HttpServletResponse response,
			String cznr,String czsm)
	throws Exception {
		   T_S_YHCZXXinfo t_YhczxxInfoA = new T_S_YHCZXXinfo();
		try{
			t_YhczxxInfoA.setF_VC_YHID(SessionUtils.getUserCode(request));
			t_YhczxxInfoA.setF_VC_YHMC(SessionUtils.getUserName(request));
			t_YhczxxInfoA.setF_VC_YHCZNR(cznr);
			t_YhczxxInfoA.setF_VC_YHCZSM(czsm);
		}
	    catch (Exception e) {	
		throw e;
		} finally {
		}
		return t_YhczxxInfoA;
	}
	
//	动态加载数据
	public void setFilterList(List list,HttpServletResponse response) throws IOException{
		StringBuffer responseXML = new StringBuffer();
		Map map = null;
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");

		if (list != null && list.size() > 0) {
			responseXML.append("{success:true,data:{list:[");
			for (int i = 0; i < list.size(); i++) {
				map = (Map) list.get(i);
				responseXML.append("['");
				responseXML.append((String) map.get("code"));
				responseXML.append("','");
				responseXML.append((String) map.get("name"));
				if (i == list.size() - 1) {
					responseXML.append("']");
				} else {
					responseXML.append("'],");
				}
			}
			responseXML.append("]}}");				
		}else{
			responseXML.append(" {success:true,data:{list:[['','']]}}");
		}
		response.getWriter().write(responseXML.toString());
	}
	
//	动态加载数据

	public void setFilterListIn3(List list,HttpServletResponse response) throws IOException{
		StringBuffer responseXML = new StringBuffer();
		Map map = null;
		
		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");

		if (list != null && list.size() > 0) {
			responseXML.append("{success:true,data:{list:[");
			for (int i = 0; i < list.size(); i++) {
				map = (Map) list.get(i);
				responseXML.append("['");
				responseXML.append((String) map.get("value1"));
				responseXML.append("','");
				responseXML.append((String) map.get("value2"));
				responseXML.append("','");
				responseXML.append((String) map.get("value3"));
				if (i == list.size() - 1) {
					responseXML.append("']");
				} else {
					responseXML.append("'],");
				}
			}
			responseXML.append("]}}");				
		}else{
			responseXML.append(" {success:true,data:{list:[['','']]}}");
		}
		response.getWriter().write(responseXML.toString());
	}	
}
